package com.leyou.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <h3>leyou-project</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-06-04 20:32
 **/
@Slf4j
@Component
public class SmsUtils {
    @Autowired
    private SmsProperties prop;
    @Autowired
    private IAcsClient client;


    public void sendVerifyCode(String phoneNumber, String templateParam){

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", prop.getRegionID());
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", prop.getSignName());
        request.putQueryParameter("TemplateCode", prop.getVerifyCodeTemplate());
        request.putQueryParameter("TemplateParam", templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            int status = response.getHttpResponse().getStatus();
            if (status != 200) {
                log.error("[SMS服务]短信发送失败,手机号{}, 失败信息{}", phoneNumber, response.getData());
            }
            log.info("[SMS服务]短信发送成功, 手机号{}, 成功信息{}", phoneNumber, response.getData());
        } catch (ServerException e) {
            log.error("服务器异常");
        } catch (ClientException e) {
            log.error("客户端异常, 异常原因{}", e.getMessage());
        }


    }




}
